home *** CD-ROM | disk | FTP | other *** search
/ Amiga Game-Power / Amiga Game-Power.iso / anwendungen / mackie / nethack / termcap.zoo / tgetnum.c < prev    next >
C/C++ Source or Header  |  1988-07-30  |  2KB  |  110 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *            Copyright (c) 1982, Fred Fish                   *
  4.  *                All Rights Reserved             *
  5.  *                                    *
  6.  *    This software and/or documentation is released for public    *
  7.  *    distribution for personal, non-commercial use only.        *
  8.  *    Limited rights to use, modify, and redistribute are hereby    *
  9.  *    granted for non-commercial purposes, provided that all        *
  10.  *    copyright notices remain intact and all changes are clearly    *
  11.  *    documented.  The author makes no warranty of any kind with    *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular    *
  14.  *    purpose.                            *
  15.  *                                    *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20. /*
  21.  *  LIBRARY FUNCTION
  22.  *
  23.  *    tgetnum   extract numeric option from termcap entry
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *    termcap
  28.  *    ce functions
  29.  *
  30.  *  SYNOPSIS
  31.  *
  32.  *    tgetnum(id)
  33.  *    char *id;
  34.  *
  35.  *  DESCRIPTION
  36.  *
  37.  *    Returns numeric value of capability <id>, or -1 if <id>
  38.  *    is not found.    Knows about octal numbers, which
  39.  *    begin with 0.
  40.  *
  41.  */
  42.  
  43. #include <stdio.h>
  44. #include <string.h>
  45. # ifdef MSDOS
  46. # define index strchr
  47. # endif
  48.  
  49. extern char *_tcpbuf;        /* Termcap entry buffer pointer */
  50.  
  51. /*
  52.  *  PSEUDO CODE
  53.  *
  54.  *    Begin tgetnum
  55.  *        Initialize pointer to the termcap entry buffer.
  56.  *        While there is a field to process
  57.  *        Skip over the field separator character.
  58.  *        If this is the entry we want then
  59.  *            If the entry is not a numeric then
  60.  *            Return failure value.
  61.  *            Else
  62.  *            Initialize value to zero.
  63.  *            If number begins with zero then
  64.  *                Set accumulation base to 8.
  65.  *            Else
  66.  *                Set accumulation base to 10.
  67.  *            End if
  68.  *            While there is a numeric character
  69.  *                Accumulate the value.
  70.  *            End while
  71.  *            Return value.
  72.  *            End if
  73.  *        End if
  74.  *        End while
  75.  *        Return failure value.
  76.  *    End tgetnum
  77.  *
  78.  */
  79.  
  80. tgetnum(id)
  81. char *id;
  82. {
  83.     int value, base;
  84.     char *bp;
  85.     extern char *index();
  86.  
  87.     bp = _tcpbuf;
  88.     while ((bp = index(bp,':')) != NULL) {
  89.     bp++;
  90.     if (*bp++ == id[0] && *bp != NULL && *bp++ == id[1]) {
  91.         if (*bp != NULL && *bp++ != '#') {
  92.         return(-1);
  93.         } else {
  94.         value = 0;
  95.         if (*bp == '0') {
  96.             base = 8;
  97.         } else {
  98.             base = 10;
  99.         }
  100.         while (isdigit(*bp)) {
  101.             value *= base;
  102.             value += (*bp++ - '0');
  103.         }
  104.         return(value);
  105.         }
  106.     }
  107.     }
  108.     return(-1);
  109. }
  110.